﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using System.IO;

using mKoenig.PictureSlide.Properties;

namespace mKoenig.PictureSlide
{
  public partial class Main : Form
  {
    #region declarations
    private Settings setting = Settings.Default;
    private FileHandling fileHandler;

    private int screenNo;

    private DateTime startTime;

    private Render render;
    private Render.ImageData data = new Render.ImageData();

    private int actPic = -1;
    private Render.ImageData slice = null;

    private long renderMode = 1;
    private Random rand = new Random();

    #endregion

    public Main(int ScreenNo)
    {
      screenNo = ScreenNo;

      this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
      this.SetStyle(ControlStyles.UserPaint, true);
      this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false);

      InitializeComponent();
      fileHandler = FileHandling.GetInstance();
    }

    private void Main_Load(object sender, EventArgs e)
    {
      if (setting.StartRandom) actPic = rand.Next(fileHandler.Count);
      LoadImage();

      render = new Render(this.Size);
      Render.ImageData data = new Render.ImageData();

      startTime = DateTime.Now.AddMilliseconds((3000 * screenNo));
    }

    public void updateTimer()
    {
      if (startTime.Year != 0)
      {
        TimeSpan span = DateTime.Now - startTime;
        double ms = span.TotalMilliseconds;

        if (ms > (setting.TransitionTime + (setting.TimeNextPicture * 1000)))
        {
          LoadImage();
        }

        myPaint();
      }
    }

    /* protected override void OnPaint(PaintEventArgs e)
    {
      FileHandling handler = FileHandling.GetInstance();
      if (slice != null)
      {
        data.Image = slice.Image;
        data.Name = slice.Name;
        data.Date = slice.Date;
        data.Info = (actPic + 1) + " / " + handler.Count + " ";

        if (setting.PictureMode == "random")
          data.Info += "(random)";
        else
          data.Info += "(sequence)";

        TimeSpan span = DateTime.Now - startTime;
        double ms = span.TotalMilliseconds;
        render.DoRender((100.0f / setting.TransitionTime * (float)ms), (int)renderMode, e.Graphics, data, false);
      }
    }
    */

    private void myPaint()
    {
      FileHandling handler = FileHandling.GetInstance();
      if (slice != null)
      {
        data.Image = slice.Image;
        data.Name = slice.Name;
        data.Date = slice.Date;
        data.Info = (actPic + 1) + " / " + handler.Count + " ";

        if (setting.PictureMode == "random")
          data.Info += "(random)";
        else
          data.Info += "(sequence)";

        TimeSpan span = DateTime.Now - startTime;
        double ms = span.TotalMilliseconds;

        Graphics e = this.CreateGraphics();
        render.DoRender((100.0f / setting.TransitionTime * (float)ms), (int)renderMode, e, data, false);
      }
    }

    private void LoadImage()
    {
      renderMode = TransitionEnabler.NextTransition();

      //renderMode = renderMode+1;
      //if (renderMode >= Render.Count) renderMode = 0;
      //renderMode = 35;

      if (setting.PictureMode == "random")
      {
        int nextPic = actPic;
        while (actPic == nextPic)
        {
          nextPic = rand.Next(fileHandler.Count);
        }
        actPic = nextPic;
      }
      else
      {
        actPic++;
        if (actPic >= fileHandler.Count)
        {
          actPic = 0;
        }
      }

      //actPic = 30;
      slice = fileHandler.LoadImage(actPic, new Size(this.Width, this.Height));
      startTime = DateTime.Now;
    }
  }
}
